package com.lc.hongtai.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.hongtai.R;
import com.lc.hongtai.conn.JsonAccessToken;
import com.lc.hongtai.conn.JsonFindpass;
import com.lc.hongtai.conn.JsonSendcode;
import com.lc.hongtai.toast.LovelyToast;
import com.lc.hongtai.view.ClearEditText;
import com.lc.hongtai.view.CountDownTimerUtils;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMatches;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.lc.hongtai.R.id.et_01;


public class RegisterActivity extends BaseActivity2 implements View.OnClickListener {
    @Bind(et_01)
    ClearEditText et01;
    @Bind(R.id.et_02)
    EditText et02;

    @Bind(R.id.et_03)
    ClearEditText et03;
    @Bind(R.id.et_04)
    ClearEditText et04;
    @Bind(R.id.et_05)
    ClearEditText et05;
    @Bind(R.id.bt_registered)
    Button btRegistered;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_back)
    ImageView ivBack;

    @Bind(R.id.regist_bg_ll)
    LinearLayout regist_bg_ll;

    private boolean isRegistMode = true;//true 注册模式；false 找回密码
    private String codeId = "";
    private String codeMode = "reg";//默认为空，发普通短信 reg:注册短信 findpass:找回密码


    //获取验证码
    @Bind(R.id.bt_getcode)
    TextView bt_getcode;

    //验证码
    private CountDownTimer mCountDownTimer;


    //    String reg="^[a-zA-Z0-9]{6,15}$";
    String reg = "([a-zA-Z0-9]*[a-zA-Z][a-zA-Z0-9]*){5,15}";
    //  ​String pwd="1234567890abcd";

    //    ​boolean f=pwd.matches(reg);
//
    @Bind(R.id.register_xieyi)
    TextView register_xieyi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register2);
        ButterKnife.bind(this);
        if ((getIntent().getIntExtra(LoginActivity.FLAG, 0)) == 2) {
            isRegistMode = false;
            codeMode = "findpass";
            tvTitle.setText(getResources().getText(R.string.register_title_forgot));

            btRegistered.setText("确定");

            regist_bg_ll.setBackgroundResource(R.mipmap.forget_bg);


        }


        btRegistered.setOnClickListener(this);
        //获取验证码
        bt_getcode.setOnClickListener(this);
        btRegistered.setSelected(false);
        register_xieyi.setOnClickListener(this);
        //验证码工具类
        mCountDownTimer = new CountDownTimerUtils(bt_getcode, 60000, 1000);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.bt_getcode:

                if (et01.getText().toString().equals("")) {

                    LovelyToast.makeText(RegisterActivity.this, "请输入手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(et01.getText().toString())) {

                    LovelyToast.makeText(RegisterActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else {
                    sentCode();
                }
                break;

            case R.id.bt_registered:
                if (TextUtils.isEmpty(et01.getText().toString().trim())) {
                    LovelyToast.makeText(RegisterActivity.this, "手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(et01.getText().toString())) {

                    LovelyToast.makeText(RegisterActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (TextUtils.isEmpty(et02.getText().toString().trim())) {
                    LovelyToast.makeText(RegisterActivity.this, "验证码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (TextUtils.isEmpty(et03.getText().toString().trim())) {
                    LovelyToast.makeText(RegisterActivity.this, "密码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (TextUtils.isEmpty(et04.getText().toString().trim())) {
                    LovelyToast.makeText(RegisterActivity.this, "确认密码不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (et03.getText().toString().length() < 6) {
                    LovelyToast.makeText(RegisterActivity.this, "密码位数不能少于6位", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!et03.getText().toString().equals(et04.getText().toString())) {
                    LovelyToast.makeText(RegisterActivity.this, "两次输入的密码不相同", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } /*else if (!et02.getText().toString().matches(reg)) {
                    LovelyToast.makeText(RegisterActivity.this, "输入的密码不正确必须包含字母和数字", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }*/ else {


                    Intent intent = new Intent();
                    intent.putExtra("phone", et01.getText().toString());

                    intent.putExtra("password", et04.getText().toString());
                    intent.putExtra("recommender", et05.getText().toString());
                    //  startActivity(new Intent(RegisterActivity.this,RegisterNextActivity.class));

                    intent.setClass(this, RegisterNextActivity.class);

                    startActivity(intent);
                }


                break;

            case R.id.register_xieyi:
                startActivity(new Intent(RegisterActivity.this,RegisterXieyiActivity.class));
                break;


        }
    }

    private void sentCode() {
        if (et01.getText().toString().equals("")) {

            //UtilToast.show(this, "请输入手机号");
            LovelyToast.makeText(RegisterActivity.this, "请输入手机号", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (!UtilMatches.checkMobile(et01.getText().toString())) {

            //  UtilToast.show(this, "请输入正确的手机号");
            LovelyToast.makeText(RegisterActivity.this, "请输入正确的手机号", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else {


            getSendCodeAccessToken();

        }

    }

    private void getSendCodeAccessToken() {


        new JsonAccessToken("common/sendcode", new AsyCallBack<JsonAccessToken.Info>() {

            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
            }

            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {

                initSendCode(info.access_token);

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                //   UtilToast.show(RegisterActivity.this, "安全验证失败！");
                LovelyToast.makeText(RegisterActivity.this, "安全验证失败", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);

                dismissLoading();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                dismissLoading();
            }
        }).execute(this, true);


    }


    private void initSendCode(String access_token) {
        new JsonSendcode(access_token, et01.getText().toString(), codeMode, new AsyCallBack<JsonSendcode.Info>() {

            @Override
            public void onSuccess(String toast, int type, JsonSendcode.Info info) throws Exception {
                if (info != null) {
                    mCountDownTimer.start();
                    codeId = info.codeId;
                    btRegistered.setSelected(true);

                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {


                dismissLoading();

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {

                // UtilToast.show(RegisterActivity.this, JsonSendcode.TOAST);
                LovelyToast.makeText(RegisterActivity.this, JsonSendcode.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }

        }).execute(this, true);
    }


    private void initFindPass(String access_token) {
        new JsonFindpass(access_token, et01.getText().toString(), et02.getText().toString(), codeId, et03.getText().toString(), new AsyCallBack() {
            @Override
            public void onSuccess(String toast, int type, Object o) throws Exception {
                if (o.equals("")) {
                    finish();
                }
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                //   UtilToast.show(RegisterActivity.this, JsonFindpass.TOAST);
                LovelyToast.makeText(RegisterActivity.this, JsonFindpass.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }

        }).execute(this, true);
    }


    public void onRegist() {

        if (TextUtils.isEmpty(et01.getText().toString().trim())) {

            LovelyToast.makeText(RegisterActivity.this, "手机号不能为空", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (!UtilMatches.checkMobile(et01.getText().toString())) {

            //UtilToast.show(this, "请输入正确的手机号");
            LovelyToast.makeText(RegisterActivity.this, "请输入正确的手机号", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (TextUtils.isEmpty(codeId)) {

            // UtilToast.show(this, "请点击获取验证码");
            LovelyToast.makeText(RegisterActivity.this, "请点击获取验证码", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (TextUtils.isEmpty(et02.getText().toString().trim())) {

            LovelyToast.makeText(RegisterActivity.this, "验证码不能为空", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (TextUtils.isEmpty(et03.getText().toString().trim())) {
            LovelyToast.makeText(RegisterActivity.this, "密码不能为空", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (et03.getText().toString().length() < 6 || et03.getText().toString().length() > 15) {
            LovelyToast.makeText(RegisterActivity.this, "请输入6~15位的密码", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (TextUtils.isEmpty(et04.getText().toString().trim())) {
            LovelyToast.makeText(RegisterActivity.this, "确认密码不能为空", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (!et03.getText().toString().trim().equals(et04.getText().toString().trim())) {

            LovelyToast.makeText(RegisterActivity.this, "两次密码输入不一致", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else {

            getAccessToken();

        }
    }

    private void getAccessToken() {
        new JsonAccessToken("common/reg", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                //  initReg(info.access_token);
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                //UtilToast.show(RegisterActivity.this, "安全验证失败！");
                LovelyToast.makeText(RegisterActivity.this, "安全验证失败！", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
                dismissLoading();
            }
        }).execute(this, true);

    }


}
